<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Banner extends Api
{
    protected $noNeedLogin = ['bannerLists', "getWechatShareImage"];
    protected $noNeedRight = ['*'];

    /**
     * 获取所有有效的轮播图
     */
    public function bannerLists()
    {
        // 查询所有状态为有效的轮播图
        $banners = Db::name('banner')
            ->where('switch = 1') // 假设 1 为有效状态
            ->order('weigh DESC') // 按照排序字段升序排列
            ->select();

        // 检查是否有数据
        if (empty($banners)) {
            $this->success("暂无轮播图", []);
        }

        $url = Db::name("config")->where(['name' => 'url'])->value('value');

        // 格式化响应数据
        $formattedBanners = [];
        foreach ($banners as $banner) {
            $formattedBanners[] = [
                'id' => $banner['id'],
                'title' => $banner['title'],
                "jump_type_list" => $banner["jump_type_list"], // 0=不跳转,1=内链,2=外链,3=自定义内容 4咨询
                'jump_url' => $banner['jump_url'], // 点击后的跳转链接
                'image' => $url . $banner['image'], // 图片地址
                "consult_manage_id" => $banner["consult_manage_id"], //咨询列表
            ];
        }

        // 返回 JSON 响应
        $this->success("暂无轮播图", $formattedBanners);
    }

    //获取分享封面
    public function getWechatShareImage()
    {
        $url = Db::name("config")->where(['name' => 'url'])->value('value');
        $image = Db::name('config')->where(['name' => 'wechat_share_image'])->value('value');
        if ($image) {
            $this->success("获取成功", $url . $image);
        } else {
            $this->error("暂无图片");
        }
    }

}
